package ComDirectBehaviour;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import Message.MEnnemie;
import Message.MState;
import Message.ReqReparation;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import Goal.GoalBase;
import Perception.PerceptsBase;
import Perception.Percepts;
import Message.MNoeud;
import Message.MEnnemie;
import Message.MReparationOK;

import java.util.HashSet;
/**
 * TODO description
 */
public class ComInfoSab extends Behaviour {

	
	private PerceptsBase percepts;
	private GoalBase goals;
	private boolean verbose;
	private AID aids[];
	private String name;
	private String teamName;
	private boolean reparation;
    private int vieOld;
	public ComInfoSab (String name, String teamName, PerceptsBase percepts, GoalBase goals,AID aids[], boolean verbose)
	{
		  init(name,teamName,percepts,goals,aids,verbose);	
	}
			
	public void init(String name,String teamName,PerceptsBase percepts, GoalBase goals,AID aids[], boolean verbose)
	{
		this.percepts = percepts;
		this.goals = goals;
		this.verbose = verbose;
		this.aids = aids;
		this.name= name;
		this.teamName = teamName;
		reparation = false;
	}	
	
	public void action()
	{
		receiveMsg();
		checkPercepts();
	}
	
	public boolean done()
	{
		return false;
	}
	
	public void checkPercepts()
	{
		boolean help = false;
		if(percepts.perceptsExists("state"))
		{
			Percepts state = percepts.getPercepts("state").get(0);
			int health = Integer.parseInt((String)(state.getParam("health").getValue()));
			int maxHealth = Integer.parseInt((String)(state.getParam("maxHealth").getValue()));
			boolean status = (health == 0);
			String pos = (String)state.getParam("position").getValue();
			String lastAction = (String)state.getParam("lastAction").getValue();
			String result = (String)state.getParam("lastActionResult").getValue();
			
			if(vieOld == -1)
			{
				vieOld = health;
				help = false;
			}
			else
			{
				help = (health < vieOld);
			}
			vieOld = health;
			MState msgState = new MState(name,pos,health,status);
			ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
			msg.setConversationId("State");
			for(int i = 0; i<aids.length;i++)
				msg.addReceiver(aids[i]);
			getAgent().send(msg);				
			
			if(health < maxHealth)
			{
				ReqReparation req = new ReqReparation(name, pos);
				ACLMessage msgH = new ACLMessage(ACLMessage.INFORM);
				msgH.setConversationId("Req-R");
				for(int i = 0; i<aids.length;i++)
					msgH.addReceiver(aids[i]);
				getAgent().send(msgH);
				reparation=true;
			}
			else if(reparation)
			{
				reparation = false;
				MReparationOK req = new MReparationOK(name);
				ACLMessage msgH = new ACLMessage(ACLMessage.INFORM);
				msgH.setConversationId("Rep-Ok");
				for(int i = 0; i<aids.length;i++)
					msgH.addReceiver(aids[i]);
				getAgent().send(msgH);
			}
			
		}
		if(percepts.perceptsExists("visibleEntity"))
		{
			for(Percepts p : percepts.getPercepts("visibleEntity"))
			{
				if(p.getParam("team").getValue().equals(teamName))
					continue;
					try 
					{
						
						ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
						boolean dis = p.getParam("status").getValue().equals("disabled");
						MEnnemie ennemie = new MEnnemie((String)p.getParam("node").getValue(),(String)(p.getParam("name").getValue()),"",(String)(p.getParam("status").getValue()),-1,(!dis&&help));
						msg.setConversationId("Target");
						msg.setContentObject(ennemie);
						
						for(int i = 0;i<aids.length;i++)
							msg.addReceiver(aids[i]);
						
						getAgent().send(msg);
					}
					catch(IOException e)
					{
						System.err.println(e.getMessage());
					}
			}
		}
	}

	public void receiveMsg() 
	{
		ACLMessage msg = getAgent().receive();
		
		while(msg != null)
		{
			checkMessage(msg);
			msg =getAgent().receive();
		}
	}
	
	public void checkMessage(ACLMessage msg)
	{
		
	}

	
}